package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.adapter.AddressSelectAdapter;
import com.qeeyou.apps.android.craftsman.common.Contants;
import com.qeeyou.apps.android.craftsman.po.AreaInfo;
import com.qeeyou.apps.android.craftsman.po.UserAddressInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.service.UserService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressActivity extends Activity {

    private ImageView btnBack;                    //返回按钮
    private ListView lstvAddress;                 //地址列表
    private LinearLayout btnAddAddress;           //添加地址

    private AddressSelectAdapter mAdapter;
    private List<UserAddressInfo> mAddressList = new ArrayList<UserAddressInfo>();    //地址列表

    private List<AreaInfo> mAreaList = new ArrayList<AreaInfo>();

    private CustomLoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);

        setView();

        setEvent();
    }

    private void setView() {
        btnBack = (ImageView) this.findViewById(R.id.btnBack4Address);
        lstvAddress = (ListView) this.findViewById(R.id.lstvAddress4Address);

        mAdapter = new AddressSelectAdapter(AddressActivity.this, mAddressList);
        lstvAddress.setAdapter(mAdapter);

        View view = LayoutInflater.from(AddressActivity.this).inflate(R.layout.view_address_footer, null, false);
        btnAddAddress = (LinearLayout) view.findViewById(R.id.btnAdd4Address);
        lstvAddress.addFooterView(view, null, false);
    }

    private void setEvent() {
        btnBack.setOnClickListener(onBackClickListener);

        lstvAddress.setOnItemClickListener(onItemClickListener);

        btnAddAddress.setOnClickListener(onAddClickListener);

        mAdapter.setOnClickListener(new AddressSelectAdapter.OnClickListener() {
            @Override
            public void onEditClick(int position) {
                //TODO 修改逻辑貌似不支持
//                startActivityForResult(new Intent(AddressActivity.this, AddAddressActivity.class)
//                                .putExtra(Contants.Extra.EXTRA_ADDRESS_INFO, mAddressList.get(position))
//                        , Contants.RequestCode.REQUEST_MODIFY_ADDRESS);
            }
        });

        loadData();
    }

    private View.OnClickListener onBackClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddressActivity.this.finish();
        }
    };

    private View.OnClickListener onAddClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //TODO -- 添加地址
            startActivityForResult(new Intent(AddressActivity.this, AddAddressActivity.class)
                    , Contants.RequestCode.REQUEST_MODIFY_ADDRESS);
        }
    };

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent();
            intent.putExtra(Contants.Extra.EXTRA_ADDRESS_INFO, mAddressList.get(position));
            setResult(RESULT_OK, intent);
            finish();
        }
    };

    private void loadData() {
        new GetAllAreaList().execute();
    }

    class GetAllAreaList extends AsyncTask<String, Void, List<AreaInfo>> {

        public GetAllAreaList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected List<AreaInfo> doInBackground(String... params) {
            List<AreaInfo> areaList = AppService.getInstance().getAllArea();
            return areaList;
        }

        @Override
        protected void onPostExecute(List<AreaInfo> areaList) {
            super.onPostExecute(areaList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if (areaList != null && areaList.size() > 0) {
                mAreaList = areaList;
                mAdapter.setAreaList(mAreaList);
            }

            new GetUserAddressList().execute();
        }
    }

    class GetUserAddressList extends AsyncTask<String, Void, List<UserAddressInfo>> {

        public GetUserAddressList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(AddressActivity.this);
            }
            mLoadingDialog.setMessage("正在获取用户地址列表...");
            mLoadingDialog.show();
        }

        @Override
        protected List<UserAddressInfo> doInBackground(String... params) {
            Map<String, String> paramMap = new HashMap<String, String>();
            List<UserAddressInfo> masterList = UserService.getInstance().getUserAddressList(paramMap);
            return masterList;
        }

        @Override
        protected void onPostExecute(List<UserAddressInfo> addressList) {
            super.onPostExecute(addressList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if (addressList != null && addressList.size() > 0) {
                mAddressList = addressList;
                mAdapter.setItems(mAddressList);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Contants.RequestCode.REQUEST_MODIFY_ADDRESS:
                if (resultCode == RESULT_OK) {
                    loadData();
                }
                break;
        }
    }
}
